<?php

/**
 * Controller_activitys
 * 活动安排
 * 
 */

class Controller_activitys extends MyApp {

	public function __construct()
	{
		parent::__construct();

		$this->model->staff  	 = YOU::loadModel('staff');
		$this->model->class   	 = YOU::loadModel('class');
		$this->model->student    = YOU::loadModel('student');
		$this->model->divide     = YOU::loadModel('divide');
		$this->model->user       = YOU::loadModel('user');
		$this->model->activitys  = YOU::loadModel('activitys');
		$this->model->file  	 = YOU::loadModel('file');
		$this->model->log  	     = YOU::loadModel('log');
	
	}


	/** 首页 **/
	public function actionIndex()
	{

	}

	/** 活动安排列表(activitys_type=3团支部) **/
	public function actionList ()
	{	
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 搜索
		$activitys_title	= $this->get->activitys_title;
		$from				= $this->get->from;
		$to					= $this->get->to;

		$where = "";

		if (!empty($activitys_title)){
			$where .= " AND activitys_title LIKE '%{$activitys_title}%' ";
			$this->tpl->assign("activitys_title", $activitys_title);
		}
		if (!empty($from)){
			$where .= " AND create_time >= '{$from}'";
			$this->tpl->assign("from", $from);
		}
		if (!empty($to)){
			$where .= " AND create_time <= '{$to}+1' ";
			$this->tpl->assign("to", $to);
		}
		
		// 进入哪个部门活动的入口
		$activitys_type   	    = $this->get->activitys_type;	
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_activitys` WHERE activitys_type= {$activitys_type} {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$activitys_lists = $this->db->fetchAll("SELECT * FROM `tbl_activitys` WHERE activitys_type = {$activitys_type} {$where} ORDER BY activitys_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
	
		foreach ($activitys_lists as $key => $value){
			// 提交者
			$activitys_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);
			// 转换时间格式
			$activitys_lists[$key]['create_time'] = date('Y-m-d',strtotime($value['create_time']));
			// 控制标题字数
		
			if(strlen($activitys_lists[$key]['activitys_title'])>=60){
				$activitys_lists[$key]['activitys_title']=mb_substr($value['activitys_title'],0,20,'utf8').'...';
			}
		}
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("activitys_lists", $activitys_lists);
		$this->tpl->assign("activitys_type", $activitys_type);
		$this->tpl->display('activitys/activitys.list.tpl.php');

	}
	
	/** 活动添加 **/
	public function actionAdd()
	{	
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$activitys_type         	= $this->get->activitys_type;

		if ($_POST){
			$activitys_title   	    = $this->post->activitys_title;
			$activitys_con 	        = $this->post->activitys_con;;
			$activitys_file         = $this->post->activitys_file;
			
			if (empty($activitys_title)) {
				Helper_Client::alert("请填写标题");
			}
			// 写入数据库
			$info = array(
			"user_id"               => $this->user['user_id'],
			"activitys_title"       => $activitys_title,	
			"activitys_con"         => $activitys_con,
			"activitys_type"        => $activitys_type,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_activitys", $info);
			$activitys_id = $this->db->insert_id();

			// 上传
            // 附件上传
            $file = $this->model->file->upload("activitys_file", $activitys_id, array("txt","ppt", "xls", "xlsx", "docs","docx", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

            if (!empty($file)){
                $info = array(
                    "activitys_file" => $file['fname'],
                );

                $this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");
            }
//			if (!empty($_FILES['activitys_file']['tmp_name'])){
//
//				$up_name = "{$activitys_id}.xls";
//				$up_path = UPLOAD_PATH."/teacher_in/{$up_name}";
//				copy($_FILES['activitys_file']['tmp_name'], $up_path);
//
//				$info = array(
//				"activitys_file" => $up_name,
//				);
//				$this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");
//			}

			// 跳转
			Helper_Client::location("添加活动成功", "index.php?a=activitys.list&activitys_type={$activitys_type}");
		}

		$this->tpl->assign("activitys_lists", $activitys_lists);
		$this->tpl->display('activitys/activitys.add.tpl.php');
	}
//下载
    public function actionoutput(){
        $fname = $this->get->activitys_file;
        $fpath = UPLOAD_PATH."/activitys_file/{$fname}";
        header("Content-Type:application/force-download");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        // 输出下载文件头s
        header("Content-Disposition: attachment; filename={$fname}");
        // 在线输出
        readfile($fpath);

    }

    /** 修改活动安排**/
	public function actionUpdate()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$activitys_type   	= (int)$this->get->activitys_type;
		$activitys_id   	= (int)$this->get->activitys_id;
		$activitys_data 	= $this->db->fetchRow("SELECT * FROM `tbl_activitys` WHERE `activitys_id`='{$activitys_id}'");
		if (empty($activitys_data)){
			Helper_Client::alert("活动安排不存在！");
		}

        $uid = $this->user['user_id'];

        if($activitys_data['user_id']  && ($uid !=$activitys_data['user_id']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }

		if ($_POST){
            //
			$activitys_title   	    = $this->post->activitys_title;
			$activitys_con 	        = $this->post->activitys_con;
			$activitys_file         = $this->post->activitys_file;

			if (empty($activitys_title)) {
				Helper_Client::alert("请填写标题");
			}
			// 写入数据库
			$info = array(
			"activitys_title"       => $activitys_title,
			"activitys_con"         => $activitys_con,
			//"activitys_file"        => $activitys_file,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",
			);
            $this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");
            $is = $this->post->activitys_file_re;
            if ($is != 0){
                // 附件上传
                $file = $this->model->file->upload("activitys_file", $activitys_id, array("txt","ppt", "xls", "xlsx", "docs","docx", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

                if (!empty($file)){
                    $info = array(
                        "activitys_file" => $file['fname'],
                    );

                    $this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");

                }

            }


			// 跳转
			Helper_Client::location("修改成功", "index.php?a=activitys.list&activitys_type={$activitys_type}");
		}

		$this->tpl->assign("activitys_data", $activitys_data);
		$this->tpl->display('activitys/activitys.update.tpl.php');
	}
	
	/** 删除活动 **/
	public function actionDel()
	{
		$activitys_id       = (int)$this->get->activitys_id;
        $activitys_data 	= $this->db->fetchRow("SELECT * FROM `tbl_activitys` WHERE `activitys_id`='{$activitys_id}'");
        if (empty($activitys_data)){
            Helper_Client::alert("活动安排不存在！");
        }

        $uid = $this->user['user_id'];

        if($activitys_data['user_id']  && ($uid !=$activitys_data['user_id']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }
		$activitys_type   	= (int)$this->get->activitys_type;
		$this->db->query("DELETE  FROM `tbl_activitys` WHERE activitys_id='{$activitys_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=activitys.list&activitys_type={$activitys_type}");
	}

	/**导入信息 **/
	public function actionIn()
	{

		if($_POST){


			// 保存上传文件到上传目录
			if (is_uploaded_file($_FILES['in_excel']['tmp_name'])){

				$up_name = "activitys.xls";
				$up_path = UPLOAD_PATH."/activitys_in/{$up_name}";

				//echo $up_path;exit;

				move_uploaded_file($_FILES['in_excel']['tmp_name'], $up_path);

				// 读取excel
				include_once("lib/phpexcel/PHPExcel.php");
				$reader = PHPExcel_IOFactory::createReader('Excel5');
				//设置以Excel5格式(Excel97-2003工作簿)
				$PHPExcel = $reader->load($up_path);
				// 载入excel文件
				$sheet = $PHPExcel->getSheet(0);
				// 读取第一個工作表
				$highestRow = $sheet->getHighestRow();
				// 取得总行数
				$highestColumm = $sheet->getHighestColumn();
				// 取得总列数
				$highestColumm = PHPExcel_Cell::columnIndexFromString($highestColumm);
				//字母列转换为数字列 如:AA变为27
				/** 循环读取每个单元格的数据 */

				for ($row = 1; $row <= $highestRow; $row++){

					$info = array();

					//行数是以第1行开始
					for ($column = 0; $column < $highestColumm; $column++) {
						//列数是以第0列开始
						$columnName = PHPExcel_Cell::stringFromColumnIndex($column);
						echo $columnName.$row.":".$sheet->getCellByColumnAndRow($column, $row)->getValue()."|";

						$info[$column] = $sheet->getCellByColumnAndRow($column, $row)->getValue();
					}
					echo "<br/><br/>";

					print_r($info);
				}


				// 处理后存入数据库

				exit();
			}
		}

		$this->tpl->display("activitys/activitys.in.tpl.php");
	}
	
	// 活动详情页
	public function actionInfo(){
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$activitys_id   = (int)$this->get->activitys_id;
		$activitys_data = $this->db->fetchROW("SELECT * FROM `tbl_activitys` WHERE `activitys_id`='{$activitys_id}'");
		// 发布人
        //$type_id = $this->model->user->getInfo($activitys_data['user_id'],'type_id');
		$name = $this->model->user->getNameById($activitys_data['user_id']);
		$activitys_data['staff_name'] = $name;
		if (empty($activitys_data)){
			Helper_Client::alert("活动安排不存在！");
		}

		$this->tpl->assign("activitys_data", $activitys_data);
		$this->tpl->display('activitys/activitys.info.tpl.php');
	}
	/** -----------------------------------班级活动 ------------------------------------- **/
	/** 班级活动列表 **/
	public function actionClassList ()
	{	
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 搜索
		$activitys_title	= $this->get->activitys_title;
		$from				= $this->get->from;
		$to					= $this->get->to;

		$where = "";

		if (!empty($activitys_title)){
			$where .= " AND activitys_title LIKE '%{$activitys_title}%' ";
			$this->tpl->assign("activitys_title", $activitys_title);
		}
		if (!empty($from)){
			$where .= " AND create_time >= '{$from}'";
			$this->tpl->assign("from", $from);
		}
		if (!empty($to)){
			$where .= " AND create_time <= '{$to}+1' ";
			$this->tpl->assign("to", $to);
		}

		// 权限
		$user = $this->user;
		// 提交人名称
		$staff_data = $this->model->staff->getinfo($user['type_id']);
	
		// 老师有哪些班级
		$appoint_lists = $this->db->fetchAll("SELECT * FROM `tbl_appoint` WHERE staff_id='{$user['type_id']}' AND appoint_type = '4' ");
		// 获得班级的名称
		foreach ($appoint_lists as $key => $value){
			// 班级名称
			$class_data = $this->model->class->getinfo($value['class_id']);
			$appoint_lists[$key]['class_name'] = $class_data['class_name'];
		}
		// 获得班级的id
		$class_id   = (int)$this->get->class_id;
		if(empty($class_id)){
			foreach ($appoint_lists as $key => $value){
				$class_id = $appoint_lists[0]['class_id'];
			}
		}

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_activitys` WHERE activitys_type='2' AND class_id = {$class_id} {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		// activitys_type=2 班级活动
		$activitys_lists = $this->db->fetchAll("SELECT * FROM `tbl_activitys` WHERE activitys_type='2' AND class_id ={$class_id} {$where} ORDER BY activitys_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($activitys_lists as $key => $value){
			// 转换时间格式
			$activitys_lists[$key]['create_time'] = date('Y-m-d',strtotime($value['create_time']));
			// 提交者
			$activitys_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);
		
		}
	
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		
		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("activitys_lists", $activitys_lists);
		$this->tpl->assign("appoint_lists", $appoint_lists);
		$this->tpl->assign("class_id", $class_id);
		
		$this->tpl->display('activitys/activitys.class.list.tpl.php');

	}
	/** 班级活动添加 **/
	public function actionClassAdd()
	{	
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 老师有哪些班级
		$appoint_lists = $this->db->fetchAll("SELECT * FROM `tbl_appoint` WHERE staff_id='{$this->user['type_id']}' AND appoint_type = '4' ");
		
		// 获得班级的名称
		foreach ($appoint_lists as $key => $value){
			// 班级名称
			$class_data = $this->model->class->getinfo($value['class_id']);
			$appoint_lists[$key]['class_name'] = $class_data['class_name'];
		}
		// 获得班级的id
		$class_id   = (int)$this->get->class_id;
		if(empty($class_id)){
			foreach ($appoint_lists as $key => $value){
				$class_id = $appoint_lists[0]['class_id'];
			}
		}

		if ($_POST){

			$activitys_title		= $this->post->activitys_title;
			$activitys_con     		= $this->post->activitys_con;
			$class_id   	        = $this->post->class_id;
			if (empty($activitys_title)) {
				Helper_Client::alert("请填写标题");
			}
			

			// 写入数据库
			$info = array(

			"activitys_title"		=> $activitys_title,
			"activitys_con"   		=> $activitys_con,
			"class_id"   			=> $class_id,
			// 班级活动内容=2
			"activitys_type"  		=> 2,			
			"user_id"  				=> $this->user['user_id'],
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_activitys", $info);
			$activitys_id = $this->db->insert_id();
		
			// 附件上传
			$file = $this->model->file->upload("activitys_file", $activitys_id, array("txt","ppt", "xls", "xlsx", "docs","docx","docx", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));
			
			if (!empty($file)){
				$info = array(
				"activitys_file" =>$file['fname'],
				);
				$this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");
			}

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=activitys.classlist&class_id={$class_id}");

		}
	
		$this->tpl->assign("class_id", $class_id);
		$this->tpl->assign("appoint_lists", $appoint_lists);
		$this->tpl->display('activitys/activitys.class.add.tpl.php');
	}
	/** 班级活动修改 **/
	public function actionClassUpdate()
	{	
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 权限
		$user = $this->user;	
		// 老师有哪些班级
		$appoint_lists = $this->db->fetchAll("SELECT * FROM `tbl_appoint` WHERE staff_id='{$user['type_id']}' AND appoint_type = '4' ");
		// 获得班级的名称
		foreach ($appoint_lists as $key => $value){
			// 班级名称
			$class_data = $this->model->class->getinfo($value['class_id']);
			$appoint_lists[$key]['class_name'] = $class_data['class_name'];
		}
		// 获得班级的id
		$class_id   = (int)$this->get->class_id;
		if(empty($class_id)){
			foreach ($appoint_lists as $key => $value){
				$class_id = $appoint_lists[0]['class_id'];
			}
		}
		$activitys_id		= $this->get->activitys_id;
		// 读取信息
		$activitys_data     = $this->model->activitys->getrow($activitys_id);

		if ($_POST){

			$activitys_title		= $this->post->activitys_title;
			$activitys_con     		= $this->post->activitys_con;
			$class_id   	        = $this->post->class_id;
			if (empty($activitys_title)) {
				Helper_Client::alert("请填写标题");
			}
			

			// 写入数据库
			$info = array(

			"activitys_title"		=> $activitys_title,
			"activitys_con"   		=> $activitys_con,
			"class_id"   			=> $class_id,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");
			// 附件上传
			if ($this->post->activitys_file_re=='1'){
				// 附件上传
				$file = $this->model->file->upload("activitys_file", $activitys_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));
			
				if (!empty($file)){
					$info = array(
					"activitys_file" => $file['fname'],
					);

					$this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");
				}
			}

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=activitys.classlist&class_id={$class_id}");
		}

		$this->tpl->assign("activitys_data", $activitys_data);
		$this->tpl->assign("appoint_lists", $appoint_lists);
		$this->tpl->display('activitys/activitys.class.update.tpl.php');
	}
	/** 班级活动删除 **/
	public function actionClassDel()
	{
		$class_id	    	= $this->get->class_id;
		$activitys_id		= $this->get->activitys_id;
		$this->db->query("DELETE  FROM `tbl_activitys` WHERE activitys_id='{$activitys_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=activitys.classlist&class_id={$class_id}");
	}
	/** 班级活动详情 **/
	public function actionClassDetails()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$class_id	    	= $this->get->class_id;
		$activitys_id		= $this->get->activitys_id;
		// 读取信息
		$activitys_data     = $this->model->activitys->getrow($activitys_id);
		// 班级名称
		$class_data = $this->model->class->getinfo($activitys_data['class_id']);
		$activitys_data['class_name']= $class_data['class_name'];
		// 转化时间
		//$activitys_data['create_time'] = date('Y-d-m',strtotime($activitys_data['create_time']));

		$this->tpl->assign("activitys_data", $activitys_data);
		$this->tpl->display('activitys/activitys.class.details.tpl.php');
	}
	// 下载
//    public function actionoutput(){
//
//        $fname	  = $this->get->fname;
//        $fpath = UPLOAD_PATH."/activitys_file/{$fname}";
//        header("Content-Type:application/force-download");
//        header("Content-Type:application/octet-stream");
//        header("Content-Type:application/download");
//        // 输出下载文件头s
//        header("Content-Disposition: attachment; filename={$fname}");
//        // 在线输出
//        readfile($fpath);
//
//    }

}

?>